C Array

 

Arrays

 

Arrays is kind of data structure that can store a fixed-size sequential collection of elements of the same type. An array is used to store a collection of data, but it is often more useful to think of an array as a collection of variables of the same type.

Arrays are used to store multiple values in a single variable, instead of declaring separate variables for each value.

All arrays consist of contiguous memory locations. The lowest address corresponds to the first element and the highest address to the last element.

To create an array,  define the data type and specify the name of the array followed by square brackets [ ].

int myNumbers[] = {255075100};





All arrays consist of contiguous memory locations. The lowest address corresponds to the first element and the highest address to the last element.

Declaring Arrays

To declare an array in C, a programmer specifies the type of the elements and the number of elements required by an array as follows −

type arrayName [ arraySize ];

This is called a single-dimensional array. The array Size must be an integer constant greater than zero and type can be any valid C data type.

 For example, to declare a 10-element array called balance of type int ,

Eg.

int balance[10];

 

Initializing Arrays

You can initialize an array in C either one by one or using a single statement as follows −

int balance[5] = {1000.0, 2.0, 3.4, 7.0, 50.0};

The number of values between braces { } cannot be larger than the number of elements that we declare for the array between square brackets [ ].

Following is an example to assign a single element of the array

balance[4] = 50.0;

Accessing Array Elements

An element is accessed by indexing the array name. This is done by placing the index of the element within square brackets after the name of the array.

 For example −

int salary = balance[9];

Access the Elements of an Array

To access an array element, refer to its index number.

Array indexes start with 0: [0] is the first element. [1] is the second element, etc.

This statement accesses the value of the first element [0] in myNumbers:

Example

int myNumbers[] = {255075100};
printf(
"%d", myNumbers[0]);

// Outputs 25

Change an Array Element

To change the value of a specific element, refer to the index number:

Example

myNumbers[0] = 33;

Set Array Size

Another common way to create arrays, is to specify the size of the array, and add elements later:

Example

// Declare an array of four integers:
int myNumbers[4];

// Add elements
myNumbers[0] = 25;
myNumbers[
1] = 50;
myNumbers[
2] = 75;
myNumbers[
3] = 100;

What is One Dimensional Array (1D Array) in C

A One-Dimensional Array in C programming is a special type of variable that can store multiple values of only a single data type such as int, float, double, char, structure, pointer, etc. at a contagious location in computer memory. Here contagious location means at a fixed gap in computer memory.

A One-Dimensional Array is also known as 1D Array

datatype variable_name[size];

Once we declare the 1D Array,

Declaration and Initialization of a One Dimensional Array in C

In C programming a one dimensional array can be declared and initialized in several ways. Let’s see the different ways of initializing a 1D array.

int a[]={7,12,9};

Store Numbers in a One Dimensional Array

To store the number in each cell of the array we can use the following syntax.

array_name[index]=value;
a[0]=26;
a[1]=15;
a[2]=34;

Access Numbers in a One Dimensional Array

We can access any number stored in a 1D array using the following syntax.

array_name[index];

Access Numbers in a One Dimensional Array

We can access any number stored in a 1D array using the following syntax.

array_name[index];

Two-Dimensional Arrays

A 2D array is also known as a matrix (a table of rows and columns).

To create a 2D array of integers, take a look at the following example:

int matrix[2][3] = { {142}, {368} };

The first dimension represents the number of rows [2], while the second dimension represents the number of columns [3]. The values are placed in row-order, and can be visualized like this:


Access the Elements of a 2D Array

To access an element of a two-dimensional array, you must specify the index number of both the row and column.

This statement accesses the value of the element in the first row (0) and third column (2) of the matrix array.

Example

int matrix[2][3] = { {142}, {368} };

printf(
"%d", matrix[0][2]);  // Outputs 2

 

 

SHARE

Milan Tomic

Hi. I’m Designer of Blog Magic. I’m CEO/Founder of ThemeXpose. I’m Creative Art Director, Web Designer, UI/UX Designer, Interaction Designer, Industrial Designer, Web Developer, Business Enthusiast, StartUp Enthusiast, Speaker, Writer and Photographer. Inspired to make things looks better.

  • Image
  • Image
  • Image
  • Image
  • Image
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment

Problems Classification

  Problems Classification Problem classification is the process of assigning a level, based on predefined criteria. Problem classification h...